package a_reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Cheng-02
 * @version V1.0
 * @Package a_reflect
 * @date 2024/4/14 下午4:37
 */
public class Test06 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        try {
            Person person = new Person();
            Class<Person> personClass = Person.class;
            Person person1 = personClass.newInstance();

            Method setName = personClass.getMethod("setName", String.class);
            setName.invoke(person1, "张三");
            System.out.println(person1);//Person{name='张三', age=0}

            Method getName = personClass.getMethod("getName");
            Object invoke = getName.invoke(person1);//person.getname
            System.out.println(invoke);


        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
